<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
if (!isset($_SESSION['member_id']))
    echo"<script type='text/javascript'>top.window.location='index.php';</script>";
if (isset($_SESSION['lang']))
    require_once "./language_files/assignReviewer_" . $_SESSION['lang'] . ".php";
else
    require_once "./language_files/assignReviewer_en.php";
?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=9" />
        <link href="style3.css" rel="stylesheet" type="text/css"/>
        <?php include 'jquery.php'; ?>
        <script type="text/javascript" src="functions.js"></script>
        <script type="text/javascript">
            //<![CDATA[
            //a function that takes the action and changes the status of the submit button from
            //disabled to enabled or vice versa
            $(document).ready(function(){
                check_top();
            });
            function change(e)
            {
                document.form.send_button.disabled=true;
                if(e.checked==true)
                {
                    document.form.send_button.disabled=false;
                }
                if(e.checked==false)
                {
                    document.form.send_button.enabled=false;
                }
            }
            //]]>
        </script>
        <title> <?php echo $lang['Assign_reviewer']; ?> </title>
    </head>
    <body>
        <form name= "form" action = "" method = "post">
            <?php
            include "functions.php";
            //check if the values of the paperID, the conferenceID and the paper title were passed from the previous page
            if (isset($_GET['paper_id']) & isset($_GET['conference_id'])) {
                $paperID = intval($_GET['paper_id']); //store the value of the paperID from previous page
                $confID = intval($_GET['conference_id']); //store the value of the conferenceID from the previous page
                //calling function assignReviewer to execute it if conditions were satisfied
                $confname = get_conference_name($confID);
                echo "</br><span class=breadcrumbs><a href='subscriberConferencePage.php?conference_id=$confID'>{$confname}</a> > " . $lang['Assign_reviewer'] . "</span><br /><br/>";
                echo "<a href='reviewerInvitations.php?conference_id={$confID}&amp;role=reviewer'>" . $lang['inviteReviewers'] . "</a>";
                assignReviewer($paperID, $confID);
            } else {//if the user for any reason was redirected to the assignReviewer page without having the paperId, confId and paper title
                echo $lang['warning_1'];
            }

            function assignReviewer($paperID, $confID) {
                global $lang;
                $Tempcategory_id = mysql_query("select * From paper where conference_id = '$confID' AND paper_id = '$paperID' ");
                While ($row = mysql_fetch_assoc($Tempcategory_id)) {
                    $category_id = $row['category_id'];
                }
                $check_deadline = mysql_query("select * From conference_category where conference_id = '$confID'
                                                     AND category_id = '$category_id' AND submission_date >= NOW()");
                While ($row = mysql_fetch_assoc($check_deadline)) {
                    $deadline = $row['submission_date'];
                }
                if (mysql_num_rows($check_deadline)) {
                    echo
                    '<p align="center">' . $lang['wait'] . $deadline . '</p>';
                    //header("Location:userHomepage.php");
                } else {
                    //get the conference long name and display it on the top of the page
                    $conference_name = get_conference_name($confID);
                    print "<h1>{$conference_name}</h1>";
                    //get the paper title and display it under the conf name
                    $paper_title = get_paper_title($paperID);
                    print "<h2>$paper_title</h2>";
                    if (!isset($_POST['send_button'])) { //if the submit button was not yet clicked
                        //a query that selects the first name, last name and area of expertise of the reviewers of the current conference who havent exceeded the limit of papers they can review
                        $view_reviewers = mysql_query("SELECT DISTINCT M.first_name, M.last_name, M.member_id, LP.maximum_papers, LP.assigned_papers, M.member_id
                                           FROM member M INNER JOIN member_privileges MP
                                           ON M.member_id = MP.member_id
                                           INNER JOIN limit_paper LP
                                           ON M.member_id = LP.member_id
                                           INNER JOIN paper P
                                           ON P.category_id = LP.category_id
                                           WHERE MP.conference_id = '$confID'
                                           AND P.paper_id = '$paperID'
                                           AND MP.privileges_id = '1'
                                           AND LP.maximum_papers - LP.assigned_papers >= 1
                                           AND M.member_id NOT IN (SELECT AP.member_id
                                                                   FROM assigned_papers AP
                                                                   WHERE AP.paper_id = '$paperID'
                                                                   AND AP.conference_id = '$confID') ")
                                or die(mysql_error());
                        //if there are no remaining reviewers to assign papers to in the db
                        if (!mysql_num_rows($view_reviewers)) {
                            echo "<p align='center'><{$lang['warning_2']}</p>";
                        } else if ($view_reviewers) {
                            echo "
                                <p>{$lang['title']}</p>
                            ";
                            //create table that displays the names and areas of expertise of all the reviewers in this conference that have not exceeded the maximum limit of papers and that are not already assigned to this paper
                            print "<table>";
                            print "<tr>";
                            echo "<th>" . $lang['check'] . "</th>";
                            print "<input type='hidden' value='Member ID' />";
                            echo "<th>{$lang['Reviewer_Name']}</th>";
                            echo "<th>" . $lang['remaing'] . "</th>";
                            print "</tr><br/>";
                            $counter = 0;
                            //fetching member id (to be the value of the checkbox),first name, last name and area of expertise from database
                            while ($row = mysql_fetch_array($view_reviewers)) {
                                $counter++;
                                $memberID = $row['member_id'];
                                $f_name = formatText_safe($row['first_name']);
                                $l_name = formatText_safe($row['last_name']);
                                $remaing_papers = $row['maximum_papers'] - $row['assigned_papers'];
                                print "<tr>";
                                print "<td><input type=\"checkbox\" name=\"reviewers$counter\" value=\"$row[member_id]\" onClick=\"change(this)\"></td>\n";
                                echo "<input type='hidden' value=\"{$row['member_id']}\"/>\n";
                                echo '<td><a href="profilePage.php?member_id=' . $memberID . '"target="_blank">' . $f_name . " " . $l_name . "</a></td>\n";
                                echo "<td>{$remaing_papers}</td>\n";
                                echo "</tr>";
                            }
                            // print the table
                            print "</table>";
                            //a hidden input type to store the variable $counter but keep it invisible from the user
                            print "<input type='hidden' name='reviewers' value='$counter'/>";
                            //print the submit button that will be used later to execute $assign_paper and $update _assigned_papers, it is disabled until a checkbox or more is clicked
                            echo "<input type='submit' name='send_button' value='{$lang['button_name']}' id='toEnable' disabled='disabled'/>";
                        }
                    } else { //else if submit button was clicked
                        for ($counter = 1; $counter <= $_POST['reviewers']; $counter++) {
//loop on the array of checkboxes (important because more than 1 checkbox may be selected)

                            if (isset($_POST["reviewers$counter"])) { //make sure the box was checked
                                $memberID = $_POST["reviewers$counter"]; //assign the value of the box checked to a variable $var
//execute query that assigns a paper to one or more reviewer by adding a row in table assigned_papers

                                $check_reviewers = mysql_query("SELECT *
                                           FROM  limit_paper LP
                                           WHERE LP.member_id = '$memberID'
                                           AND LP.maximum_papers - LP.assigned_papers >= 1
                                           AND LP.member_id NOT IN (SELECT AP.member_id
                                                                   FROM assigned_papers AP
                                                                   WHERE AP.paper_id = '$paperID'
                                                                   AND AP.conference_id = '$confID') ") or die(mysql_error());
                                if (!mysql_num_rows($check_reviewers)) {
                                    echo $lang['warning_5'];
                                } else {
                                    $assign_paper = mysql_query("INSERT INTO assigned_papers (member_id, conference_id, paper_id)
                                                    VALUES ('$memberID', '$confID', '$paperID')")
                                            or die(mysql_error());
//the query updates the assigned papers of the reviewer to become assigned papers + 1
                                    $update_assigned_papers = mysql_query("UPDATE limit_paper
                                                    SET assigned_papers = assigned_papers + 1
                                                    WHERE member_id = '$memberID'
                                                    AND maximum_papers - assigned_papers >=1
                                                    AND conference_id = '$confID'
                                                    AND category_id IN (SELECT DISTINCT C.category_id
                                                                        FROM category C INNER JOIN paper P
                                                                        ON C.category_id = P.category_id
                                                                        WHERE C.category_id = P.category_id
                                                                        AND P.paper_id = '$paperID')") or die(mysql_error());
                                    $checkStart_date = mysql_query("SELECT * FROM conference WHERE conference_id ='$confID' AND rev_startDate =0000 -00 -00");
                                    if (mysql_num_rows($checkStart_date)) {
                                        $update_assigned_papers = mysql_query("UPDATE conference
                                                                           SET rev_startDate = NOW()
                                                                           Where conference_id ='$confID'");
                                    }
                                    //end for loop
                                    //if both queries were executed successfuly
                                    if ($assign_paper && $update_assigned_papers) {
                                        echo $lang['warning_3'];
                                        //if there was an error in executing queries
                                    } else {
                                        echo "<p>{$lang['warning_4']}" . mysql_error() . "</p>";
                                    }
                                    //close connection
                                    mysql_close();
                                }
                            }
                        }
                    }
                }
            }
            ?>
        </form>
    </body>
</html>
